﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace HFController
{
    /// <summary>
    /// 文集，随笔，成功案例业务逻辑
    /// </summary>
    public class ArticleController:ControllerBase
    {
        /// <summary>
        /// 单列模式
        /// </summary>
        private static ArticleController _articleController;

        public static ArticleController getController()
        {
            if(_articleController==null)
            {
                _articleController = new ArticleController();
            }
            return _articleController;
        }
        /// <summary>
        /// 数据库访问
        /// </summary>
        private hf.DAL.Article ArticleDAL = new hf.DAL.Article();

        /// <summary>
        /// 得到置顶和推荐首页，指定数量的列表
        /// </summary>
        /// <param name="Count"></param>
        /// <param name="isTop"></param>
        /// <param name="isIndex"></param>
        /// <returns></returns>
        public List<hf.Model.Article> getArticleList(int Count, int isTop, int isIndex)
        {
            List<hf.Model.Article> list = new List<hf.Model.Article>();
            //记录总条数
            int total;
            //筛选条件
            string strSql = "isTop = " + isTop.ToString() + " and isIndex=" + isIndex.ToString();
            //获得列表
            list = ArticleDAL.GetArticleList(Count, 1, strSql, out total);

            return list;
        }
        /// <summary>
        /// 得到列表
        /// </summary>
        /// <param name="PagerStr"></param>
        /// <returns></returns>
        public List<hf.Model.Article> getArticleList(out string PagerStr)
        {
            List<hf.Model.Article> list = new List<hf.Model.Article>();

            //没有显示记录
            int PageSize = 10;
            //当前页码
            int PageNumber = 1;
            if (HttpContext.Current.Request.QueryString["Page"] != null)
            {
                PageNumber = int.Parse(HttpContext.Current.Request.QueryString["Page"].ToString());
            }
            //记录总条数
            int total;
            //筛选条件
            string strSql = "";

            //获得列表
            list = ArticleDAL.GetArticleList(PageSize, PageNumber, strSql, out total);

            //分页
            PagerStr = LLL.Pager.TrunPageHtmlCode(PageSize, PageNumber, total, "");

            return list;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageNumber"></param>
        /// <param name="strWhere"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public List<hf.Model.Article> getArticleList(int PageSize ,int PageNumber ,string strWhere ,out int total)
        {
            List<hf.Model.Article> list = new List<hf.Model.Article>();


            //获得列表
            list = ArticleDAL.GetArticleList(PageSize, PageNumber, strWhere, out total);
            return list;
        }

        /// <summary>
        /// 保存文章
        /// </summary>
        /// <param name="title"></param>
        /// <param name="articletypeid"></param>
        /// <param name="content"></param>
        /// <param name="pictitle"></param>
        /// <returns></returns>
        public HFModel.ResultModel doAddArticle(string title, string articletypeid, string content, string pictitle)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            hf.Model.Article model = new hf.Model.Article();
            BagePages bp = new BagePages();
            hf.Model.Lawyer lawyer = bp.getLoginLawyer();
            if (lawyer == null)
            {
                result.isTrue = false;
                result.Message = "请先登录";
            }
            else {
                model.Addtime = DateTime.Now;
                model.ArticleTypeID = int.Parse(articletypeid);
                model.Author = lawyer.RealName;
                model.Content = content;
                model.Hits = 0;
                model.isIndex = 0;
                model.isTop = 0;
                model.LawyerID = lawyer.ID;
                model.PicTitle = pictitle;
                model.Title = title;
                try {
                    ArticleDAL.Add(model);
                    result.isTrue = true;
                    result.Message = "发布成功！";
                }
                catch (Exception err) {
                    result.isTrue = false;
                    result.Message = err.Message;
                }
            }
            return result;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ID"></param>
        public void doDelegeArticle(int ID)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();

            try
            {
                ArticleDAL.Delete(ID);

                result.isTrue = true;
                result.Message = "删除成功！";
                result.goUrl = "location='CorpusList.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="IDS"></param>
        public void doDelegeArticle(string[] IDS)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            try
            {
                for (int i = 0; i < IDS.Length; i++)
                {
                    ArticleDAL.Delete(int.Parse(IDS[i].Trim()));
                }
                result.isTrue = true;
                result.Message = "删除成功！";
                result.goUrl = "location='CorpusList.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }
    }
}
